package com.typy.www.tpay.util;

import android.content.Context;
import android.graphics.DashPathEffect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.*;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.LargeValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Utils;
import com.typy.www.tpay.R;
import com.typy.www.tpay.ui.view.MyMarkerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 13799 on 2018/7/15.
 */

public class MpUtil {


    public static void initMp(LineChart mChart) {

        //设置手势滑动事件
//       mChart.setOnChartGestureListener(this);
////       //设置数值选择监听
//       mChart.setOnChartValueSelectedListener(this);
        //后台绘制
        mChart.setDrawGridBackground(false);

        //设置描述文本
        mChart.getDescription().setEnabled(false);
        //设置支持触控手势
        mChart.setTouchEnabled(true);
        //设置缩放
        mChart.setDragEnabled(false);
        //设置推动
        mChart.setScaleEnabled(false);
        //如果禁用,扩展可以在x轴和y轴分别完成
        mChart.setPinchZoom(true);
        //x轴
        LimitLine llXAxis = new LimitLine(10f, "标记");
        //设置线宽
        llXAxis.setLineWidth(4f);
        //设置
        llXAxis.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
        llXAxis.setTextSize(10f);
        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGranularity(1f);
        //设置x轴的最小值mType==
        xAxis.setAxisMinimum(0f);
        mChart.getAxisLeft().setEnabled(false);
        mChart.getXAxis().setDrawGridLines(false);
        mChart.getAxisLeft().setDrawGridLines(false);
        YAxis leftAxis = mChart.getAxisLeft();
        //重置所有限制线,以避免重叠线
        leftAxis.removeAllLimitLines();
        leftAxis.setDrawZeroLine(false);
        // 限制数据(而不是背后的线条勾勒出了上面)
        leftAxis.setDrawLimitLinesBehindData(true);
        mChart.getAxisRight().setEnabled(false);
        //默认动画
        mChart.animateX(100);
        //刷新
        // 得到这个文字
        Legend l = mChart.getLegend();
        // 修改文字 ...
        l.setForm(Legend.LegendForm.LINE);
    }

    public static void setData(Context context,LineChart mChart, String lab,final List<String> xLine, List<Float> yLine) {
        MyMarkerView mv = new MyMarkerView(context, R.layout.custom_marker_view);
        mv.setChartView(mChart); // For bounds control
        mChart.setMarker(mv); // Set the marker to the chart
        for(int i=0;i<xLine.size();i++){
            String str=xLine.get(i).replace("2018-","");
            xLine.set(i,str);
        }
        XAxis xAxis = mChart.getXAxis();
        xAxis.setAxisMaximum(xLine.size()-1);
        if (xLine.size()>10)
            xAxis.setGranularity(3.8f);
        else
            xAxis.setGranularity(1f);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String data="";
                if((int) value<xLine.size()) {
                    if(xLine.size()>10&&xLine.size()-(int) value<4)
                        data=String.valueOf(xLine.get(xLine.size()-1));
                    else
                        data = String.valueOf(xLine.get((int) value));
                }
                return data;
            }
        });
        LineDataSet set1;
        ArrayList<Entry> values = new ArrayList<Entry>();
        float max=0f;
        float min=yLine.get(0);
        for (int i = 0; i < yLine.size(); i++) {
            values.add(new Entry(i, yLine.get(i)));
            max=yLine.get(i)>max?yLine.get(i):max;
            min=yLine.get(i)<min?yLine.get(i):min;
        }
        max=max*1.1f;
        min=min*0.9f;
        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.setAxisMinimum(min);
        leftAxis.setAxisMaximum(max);
        if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
            set1.setValues(values);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            // 创建一个数据集,并给它一个类型
            set1 = new LineDataSet(values, lab);
            set1.setValueFormatter(new LargeValueFormatter(""));
            set1.setColor(context.getResources().getColor(R.color.titlebar_bulue));
            set1.setCircleColor(context.getResources().getColor(R.color.titlebar_bulue));
            set1.setDrawFilled(true);
            set1.setFormSize(1.f);
            set1.setDrawValues(false);
            if (Utils.getSDKInt() >= 18) {
                // 填充背景只支持18以上
                set1.setFillColor(context.getResources().getColor(R.color.titlebar_bulue));
            } else {
                set1.setFillColor(context.getResources().getColor(R.color.titlebar_bulue));
            }
            ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
            //添加数据集
            dataSets.add(set1);
            //创建一个数据集的数据对象
            LineData data = new LineData(dataSets);
            mChart.setData(data);
        }
    }
}
